<template>
  <div class="app-container">
    <!--  -->
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
      class="query-form"
    >
      <el-form-item label="人员名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入预案名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="content-container" style="padding:0;background-color: transparent; flex-direction: row;">
      <div style="width:300px;height: 100%;border: 1px solid #eee;background-color: #fff;">
        <div class="flex justify-between align-center" style="height: 56px;border-bottom: 1px solid #eee;padding: 0 16px;">
          <span>目录</span>
          <el-button type="primary" size="mini" @click="handleAddDept">添加</el-button>
        </div>
        <Scrollbar v-loading="directoryLoading" style="height: calc(100% - 56px)">
          <div style="padding: 16px;">
            <el-tree
              ref="tree"
              class="filter-tree"
              :data="treeCommunicateData"
              :props="defaultProps"
              default-expand-all
              :expand-on-click-node="false"
              @node-click="nodeClick"
            >
              <span slot-scope="{ node, data }" class="custom-tree-node">
                <span>{{ node.label }}</span>
                <span v-if="node.level !== 1">
                  <el-button type="text" size="mini" @click="handleEditDept(data,node)">编辑</el-button>
                  <el-button class="text-danger" size="mini" type="text" @click="handleDelDept(data)">删除</el-button>
                </span>
              </span>
            </el-tree>
          </div>
        </Scrollbar>
      </div>
      <!-- right -->
      <div class="flex-1" style="position: relative;margin-left: 16px; height: 100%; overflow: hidden;border: 1px solid #eee;background-color: #fff;">
        <div class="handle_bar" style="padding-left: 16px; padding-right: 16px;">
          <div class="tit"><span>人员列表</span></div>
          <div>
            <el-button
              type="primary"
              plain
              size="small"
              @click="handleAddPerson"
            >添加</el-button>
          </div>
        </div>
        <div style="position: absolute;left: 0;top: 56px;bottom: 50px;right: 0;display: flex;flex-direction: column;overflow: hidden;">
          <el-table
            v-loading="personLoading"
            :data="personList"
            class="flex-1"
            height="250"
            style="width:100%;"
          >
            <el-table-column label="目录名称" align="center" prop="directoryName" />
            <el-table-column label="人员名称" align="center" prop="userName" />
            <el-table-column label="人员职务" align="center" prop="userDuty" />
            <el-table-column label="电话" align="center" prop="phoneNum" />
            <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  v-hasPermi="['fj:person:edit']"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdatePerson(scope.row)"
                >修改</el-button>
                <el-button
                  v-hasPermi="['fj:person:remove']"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  class="text-danger"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <pagination
          style="position: absolute;left: 0;bottom: -12px;right: 0;"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <!-- 目录 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级目录" prop="parentId">
          <treeselect v-model="form.parentId" :options="treeCommunicateData" :normalizer="normalizer" placeholder="选择上级上级目录" />
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="form.directoryName" placeholder="请输入目录名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDeptForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!--  -->
    <!-- 添加或修改通讯录人员名单对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="80px">
        <el-form-item label="目录名称" prop="directoryId">
          <treeselect v-model="form1.directoryId" :options="treeCommunicateData" :normalizer="normalizer" placeholder="选择目录" />
        </el-form-item>
        <el-form-item label="人员名称" prop="userName">
          <el-input v-model="form1.userName" placeholder="请输入人员名称" />
        </el-form-item>
        <el-form-item label="人员职务" prop="userDuty">
          <el-input v-model="form1.userDuty" placeholder="请输入人员职务" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNum">
          <el-input v-model="form1.phoneNum" placeholder="请输入电话" />
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form1.remark" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open1 = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Scrollbar from '@/components/Scrollbar'
import { updateDirectory, addDirectory, delDirectory, listDirectory } from '@/api/fjgs/warning/contacts.js'
import { listPerson, getPerson, delPerson, addPerson, updatePerson } from '@/api/fjgs/warning/contacts.js'
import { handleTree } from '@/utils/ruoyi'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
// import Tree from 'vue-giant-tree'
export default {
  components: {
    Scrollbar,
    Treeselect
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        directoryId: null,
        userName: null,
        userDuty: null,
        phoneNum: null,
        createUser: null,
        updateUser: null
      },
      // 总条数
      total: 0,
      //
      open: false,
      title: '',
      form: {},
      rules: {
        parentId: [{ required: true, message: '选择目录', trigger: 'blur' }],
        directoryName: [{ required: true, message: '请输入目录名称', trigger: 'blur' }]
      },
      directoryLoading: false,
      communicateData: [],
      //
      open1: false,
      title1: '',
      form1: {},
      rules1: {
        directoryId: [{ required: true, message: '选择目录', trigger: 'blur' }],
        userName: [{ required: true, message: '请输入人员名称', trigger: 'blur' }],
        userDuty: [{ required: true, message: '人员职务', trigger: 'blur' }],
        phoneNum: [{ required: true, message: 'phoneNum', trigger: 'blur' }]
      },
      personLoading: false,
      personList: [],
      defaultProps: {
        children: 'children',
        label: 'directoryName',
        checkStrictly: true
      }
    }
  },
  computed: {
    treeCommunicateData() {
      return handleTree(this.communicateData)
    }
  },
  created() {
    // this.listToTree(this.communicateData)
    this.getDirectoryList()
    this.getList()
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.directoryName,
        children: node.children
      }
    },
    nodeClick(node) {
      this.queryParams.directoryId = node.id
      this.queryParams.pageNum = 1
      this.getList()
      // console.log(node)
    },
    // ======= Person ======
    getList() {
      this.personLoading = true
      listPerson(this.queryParams).then(response => {
        this.personList = response.rows
        this.total = response.total
        this.personLoading = false
      })
    },
    // 表单重置
    reset1() {
      this.form1 = {
        id: null,
        directoryId: null,
        userName: null,
        userDuty: null,
        phoneNum: null,
        createUser: null,
        createTime: null,
        updateUser: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form1')
    },
    handleAddPerson() {
      this.reset1()
      this.open1 = true
      this.title1 = '添加人员'
    },
    /** 修改按钮操作 */
    handleUpdatePerson(row) {
      this.reset()
      const id = row.id
      getPerson(id).then(response => {
        console.log(response.data)
        this.form1 = response.data
        this.open1 = true
        this.title1 = '修改人员信息'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id
      this.$confirm('是否确认删除通讯录人员名称为"' + row.userName + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delPerson(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form1'].validate(valid => {
        if (valid) {
          if (this.form1.id != null) {
            updatePerson(this.form1).then(response => {
              this.msgSuccess('修改成功')
              this.open1 = false
              this.getList()
            })
          } else {
            addPerson(this.form1).then(response => {
              this.msgSuccess('新增成功')
              this.open1 = false
              this.getList()
            })
          }
        }
      })
    },
    // ======= Directory ======
    getDirectoryList() {
      this.directoryLoading = true
      listDirectory(this.queryParams).then(response => {
        this.communicateData = response.rows
        // this.total = response.total
        this.directoryLoading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        directoryName: null,
        parentId: null,
        parentName: null,
        code: null,
        createUser: null,
        createTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    handleAddDept() {
      this.reset()
      this.open = true
      this.title = '添加通讯目录'
    },
    handleEditDept(data,node) {
      this.form = {
        id: data.id,
        directoryName: data.directoryName,
        parentId: data.parentId,
        parentName: data.parentName,
        code: data.code,
        remark: data.remark
      }
      console.log(node)
      this.title = '修改通讯目录'
      this.open = true
    },
    /** 提交按钮 */
    submitDeptForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDirectory(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getDirectoryList()
            })
          } else {
            addDirectory(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getDirectoryList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelDept(row) {
      const ids = row.id
      this.$confirm('是否确认删除通讯目录为"' + row.directoryName + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delDirectory(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    }
  }
}
</script>

<style>
.custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
</style>
